$(function(){
	if(!IS_AUTH && !$_GET['post_id']){
		$_GET['user_id'] = ME_ID;
	}
	if(!$_GET['qequence']){
		$_GET['sequence'] = 'desc';
	}
	var commentListDiv = $('#comment-list-div'),
		modal = $('#comment-modal'),
		ListMenu = { //评论列表菜单
			'排序': {
				'按最新排序': function(){
					$_GET = $.extend($_GET, {page: 1, orderby: 'comment_time', sequence: 'desc'});
					location.search = '?'+$.param($_GET);
				},
				'按 ID 排序': function(){
					$_GET = $.extend($_GET, {page: 1, orderby: 'comment_id', sequence: 'asc'});
					location.search = '?'+$.param($_GET);
				}
			},
			'上一页': function(){
				$_GET['page'] -= 1;
				location.search = '?'+$.param($_GET);
			},
			'下一页': function(){
				$_GET['page'] += 1;
				location.search = '?'+$.param($_GET);
			},
			'修改': {
				'回复': function(){
					var target = commentListDiv.children('.active').eq(0);
					modal.find('form').attr('action', SITE_URL+'mod.php?comment::add').prepend('<input type="hidden" name="comment_parent" value="'+target.data('id')+'">').find('input[name="comment_id"]').remove();
					modal.find('.modal-header>h4').text('回复');
					var placeholder = '@'+target.find('.media-heading').text().replace(/\s*/g, '');
					modal.one('shown.bs.modal', function(){
						$(this).find('textarea[name="comment_content"]').val('').attr('placeholder', placeholder).focus();
					}).modal('show');
				},
				'编辑': function(){
					var target = commentListDiv.children('.active').eq(0);
					modal.find('form').attr('action', SITE_URL+'mod.php?comment::update').prepend('<input type="hidden" name="comment_id" value="'+target.data('id')+'">').find('input[name="comment_parent"]').remove();
					if(target.find('.comment-at').length == 1){
						placeholder = target.find('.comment-at').text();
					}else{
						placeholder = '写下你想说的话...';
					}
					modal.one('shown.bs.modal', function(){
						$(this).find('textarea[name="comment_content"]').attr('placeholder', placeholder).val(target.find('.comment-content').text()).focus();
					}).modal('show');
				},
				'删除': function(){
					commentListDiv.editMulti({
						obj: 'comment',
						act: 'delete',
						unit: '条',
						action: '删除',
						item: '评论'
					});
				},
				'通过审核': function(){
					commentListDiv.editMulti({
						obj: 'comment',
						act: 'update',
						data: {comment_status: 1},
						unit: '条',
						action: '审核',
						item: '评论',
						callback: function(){
							$.get(SITE_URL+'mod.php?comment::getUnreviewedCount', function(result){
								$.showNews('comments', result.success ? result.data : null);
								location.reload();
							});
						}
					});
				},
				'屏蔽此人': function(){
					var target = commentListDiv.children('.active'),
						modal = $('#comment-modal-blacklist'),
						uid = target.eq(0).data('user-id'),
						email = target.eq(0).data('user-email'),
						ip = target.eq(0).data('comment-ip');
					modal.find('input[name="blacklist_uid"]').val(uid);
					modal.find('input[name="blacklist_email"]').val(email);
					modal.find('input[name="blacklist_ip"]').val(ip);
					modal.modal('show');
				}
			}
		},
		showListMenu = function(){
			if(!TOTAL || TOTAL == 1){
				var menu = {};
			}else if($_GET['page'] == 1 && $_GET['page'] == PAGES){
				var menu ={'排序': ListMenu['排序']};
			}else if($_GET['page'] == 1){
				var menu ={
					'排序': ListMenu['排序'],
					'下一页': ListMenu['下一页']
				};
			}else if($_GET['page'] == PAGES){
				var menu ={
					'排序': ListMenu['排序'],
					'上一页': ListMenu['上一页']
				};
			}else{
				var menu = {
					'排序': ListMenu['排序'],
					'上一页': ListMenu['上一页'],
					'下一页': ListMenu['下一页']
				}
			}
			var target = commentListDiv.find('.active'),
				length = target.length,
				user_id = target.eq(0).data('user-id'),
				comment_status = target.eq(0).data('status');
			if(length){
				if(!$_GET['commentsTo']){
					var _menu = length > 1 ? {'删除': ListMenu['修改']['删除']} : {
							'编辑': ListMenu['修改']['编辑'],
							'删除': ListMenu['修改']['删除']
						};
					if(menu['上一页'] || menu['下一页']){
						if(user_id != ME_ID){
							if(IS_AUTH){
								_menu['修改'] = $.extend(length > 1 ? {} : {'回复': ListMenu['修改']['回复']}, _menu);
							}else{
								_menu =  $.extend(length > 1 ? {} : {'回复': ListMenu['修改']['回复']}, _menu);
							}
						}else{
							_menu['修改'] = $.extend(length > 1 ? {} : {'回复': ListMenu['修改']['回复']}, _menu);
							delete _menu['修改']['回复'];
						}
						delete _menu['编辑'];
						delete _menu['删除'];
						if(length > 1 && $_GET['post_id'] && !IS_AUTH){
							delete _menu['修改'];
						}
						menu = $.extend(menu, _menu);
						if(IS_AUTH){
							if(COMMENT_REVIEW && (length > 1 || comment_status != 1)){
								menu['修改']['通过审核'] = ListMenu['修改']['通过审核'];
							}
							if(length == 1) menu['修改']['屏蔽此人'] = ListMenu['修改']['屏蔽此人'];
						}
					}else{
						if(user_id != ME_ID){
							if(IS_AUTH){
								_menu['修改'] = $.extend(length > 1 ? {} : {'回复': ListMenu['修改']['回复']}, _menu);
							}else{
								_menu =  $.extend(length > 1 ? {} : {'回复': ListMenu['修改']['回复']}, _menu);
							}	
							delete _menu['编辑'];
							delete _menu['删除'];
							if(IS_AUTH){
								if(length > 1) delete _menu['修改']['回复'];
								if(COMMENT_REVIEW && (length > 1 || comment_status != 1)){
									_menu['修改']['通过审核'] = ListMenu['修改']['通过审核'];
								}
								if(length == 1) _menu['修改']['屏蔽此人'] = ListMenu['修改']['屏蔽此人'];
							}
						}
						if(length > 1 && $_GET['post_id'] && !IS_AUTH){
							delete _menu['删除'];
						}
						menu = $.extend(menu, _menu);
					}
				}else{
					var _menu = length > 1 ? {} : {'回复': ListMenu['修改']['回复']};
					if(IS_AUTH){
						_menu['编辑'] = ListMenu['修改']['编辑'];
						_menu['删除'] = ListMenu['修改']['删除'];
					}
					menu = $.extend(menu, _menu);
					if(IS_AUTH){
						menu = $.extend(menu, {'修改': _menu});
						delete menu['回复'];
						delete menu['编辑'];
						delete menu['删除'];
						if(COMMENT_REVIEW && (length > 1 || comment_status != 1)){
							menu['修改']['通过审核'] = ListMenu['修改']['通过审核'];
						}
						if(length == 1 && user_id != ME_ID) menu['修改']['屏蔽此人'] = ListMenu['修改']['屏蔽此人'];
						else delete menu['修改']['编辑'];
					}
				}
			}
			$.showMenu(menu);
		};
	showListMenu();

	/** 点击列表项时对其进行高亮 */
	commentListDiv.on('click', '.media', function(){
		$(this).toggleClass('active');
		showListMenu();
	});
	
	/** 通过网络获取 IP 地址 */
	$.get('https://ipv4.ip.nf/me.json', function(data){
		if(typeof data != 'object') data = JSON.parse(data);
		modal.find('form').find('input[name="comment_ip"]').val(data.ip.ip);
	});

	/** 提交评论表单 */
	modal.find('form').ajaxSubmit({
		success: function(result){
			if(result.success){
				if($(this).attr('action') == SITE_URL+'mod.php?comment::update'){
					commentListDiv.children('.active').find('.comment-content').text(result.data.comment_content);
				}else{
					alert('回复成功。');
					if(!$_GET['commentsTo']){
						location.reload();
					}
				}
				modal.modal('hide');
			}else{
				alert(result.data);
			}
		},
		error: function(xhr){
			alert('与服务器的连接出现错误，评论失败。');
			console.log(xhr.responseText);
		}
	});

	/** 提交黑名单表单 */
	$('#comment-modal-blacklist').on('hide.bs.modal', function(){
		$(this).find('button[type="reset"]').click();
	}).find('form').ajaxSubmit({
		beforeSend: function(){
			if($(this).find(':checked').length < 1){
				alert('请选择需要屏蔽的项目。');
				return false;
			}
			if(!confirm('屏蔽后将不会再收到此人的评论，确认？')) return false;
		},
		success: function(result){
			if(result.success){
				alert('屏蔽成功。');
				$(this).find('button[type="reset"]').click();
				$(this).closest('.modal').modal('hide');
			}else{
				alert(result.data);
			}
		},
		error: function(xhr){
			alert('与服务器的连接出现错误，屏蔽失败。');
			console.log(xhr.responseText);
		}
	}).find('button[type="reset"]').click(function(){
		var modal = $('#comment-modal-blacklist');
		modal.find('input[name="blacklist_uid"]').val('');
		modal.find('input[name="blacklist_email"]').val('');
		modal.find('input[name="blacklist_ip"]').val('');
	});

	/** 显示帮助信息 */
	$.showHelp('除了评论者本人和编辑人员，未经审核的评论不能被其他用户所查看。');
})